(function($) {
	
	// Options
	$.extend(mejs.MepDefaults, {
		timeAndDurationSeparator: '<span> | </span>'
	});


	// Time
	$.extend(MediaElementPlayer.prototype, {
		buildcurrent: function(player, controls, layers, media) {
			var t = this;
			
			$('<div class="mejs-time">'+
					'<span class="mejs-currenttime">' + (player.options.alwaysShowHours ? '00:00:00' : '00:00')
					+ '</span>'+ '</div>')
					.appendTo(controls);
			
			t.currenttime = t.controls.find('.mejs-currenttime');

                            
                        media.addEventListener('timeupdate',function() {
                            player.updateCurrent();
                            player.currentTime = media.currentTime;

                        }, false);
                            
		},


		buildduration: function(player, controls, layers, media) {
			var t = this;
			
			if (controls.children().last().find('.mejs-currenttime').length > 0) {
				$(t.options.timeAndDurationSeparator +
					'<span class="mejs-duration">' + 
						(t.options.duration > 0 ? 
							mejs.Utility.secondsToTimeCode(t.options.duration, t.options.alwaysShowHours || t.media.duration > 3600) :
				   			(player.options.alwaysShowHours ? '00:00:00' : '00:00') 
				   		) + 
					'</span>')
					.appendTo(controls.find('.mejs-time'));
			} else {

				// Add class to current time
				controls.find('.mejs-currenttime').parent().addClass('mejs-currenttime-container');
				
				$('<div class="mejs-time mejs-duration-container">'+
					'<span class="mejs-duration">' + 
						(t.options.duration > 0 ? 
							mejs.Utility.secondsToTimeCode(t.options.duration, t.options.alwaysShowHours || t.options.duration > 3600) :
				   			(player.options.alwaysShowHours ? '00:00:00' : '00:00') 
				   		) + 
					'</span>' +
				'</div>')
				.appendTo(controls);
			}
			
			t.durationD = t.controls.find('.mejs-duration');

                        media.addEventListener('timeupdate',function() {
                            player.updateDuration();
                        }, false);
		},
		
		updateCurrent:  function() {
            
			var t = this;

			if (t.currenttime) {
                            t.currenttime.html(mejs.Utility.secondsToTimeCode(t.currentTime, t.options.alwaysShowHours || (t.options.duration ? t.options.duration > 3600 : t.media.duration > 3600)));
                        }
		},
		
		updateDuration: function() {   
			var t = this;

			//Toggle the long audio class if the audio is longer than an hour.
                        if(t.options.duration > 0)
                            t.container.toggleClass("mejs-long-audio", t.options.duration > 3600);
                        else
                            t.container.toggleClass("mejs-long-audio", t.media.duration > 3600);
			
			if (t.durationD && (t.options.duration > 0 || t.media.duration > 0)) {
				t.durationD.html(mejs.Utility.secondsToTimeCode((t.options.duration ? t.options.duration : t.media.duration), t.options.alwaysShowHours));
			}	
                }
	});

})(mejs.$);
